#property copyright "Copyright 2024"
#property link      ""
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0

// 输入参数
input color UpColor = clrGreen;        // M5阳线色
input color DownColor = clrRed;        // M5阴线色
input color WickColor = clrDimGray;    // 影线颜色
input int BlockAlpha = 80;             // 色块透明度(0-255)
input int WickWidth = 1;               // 影线宽度
input ENUM_LINE_STYLE WickStyle = STYLE_SOLID;  // 影线样式
input bool ShowWickZone = false;       // 是否显示影线区域
input int NumberOfBars = 100;          // 显示的K线数量
input ENUM_TIMEFRAMES BlockTF = PERIOD_M15; // 色块K线时间框架

// 全局变量
datetime last_draw_time = 0;
string prefix = "M5Block_";           // 对象名称前缀

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   // 设置定时器
   EventSetTimer(10);
   
   // 清理旧的对象
   ObjectsDeleteAll(0, prefix);
   
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   // 删除所有创建的对象
   ObjectsDeleteAll(0, prefix);
   EventKillTimer();
}

//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
{
   DrawM5Block();
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   if(rates_total < 1) return(0);
   
   DrawM5Block();
   return(rates_total);
}

//+------------------------------------------------------------------+
//| 绘制M5 K线                                                       |
//+------------------------------------------------------------------+
void DrawM5Block()
{
   for(int i = 0; i < NumberOfBars; i++)
   {
      datetime block_time = iTime(_Symbol, BlockTF, i);
      if(block_time == 0) continue;
      datetime block_end = block_time + PeriodSeconds(BlockTF);
      double block_open = iOpen(_Symbol, BlockTF, i);
      double block_close = iClose(_Symbol, BlockTF, i);
      double block_high = iHigh(_Symbol, BlockTF, i);
      double block_low = iLow(_Symbol, BlockTF, i);

      string body_name = prefix + "Body_" + IntegerToString(block_time);
      string wick_high_name = prefix + "WickHigh_" + IntegerToString(block_time);
      string wick_low_name = prefix + "WickLow_" + IntegerToString(block_time);

      // 影线颜色
      color wick_color = clrGray;
      // K线实体颜色（不加透明度，防止变蓝）
      color body_color = (block_close >= block_open) ? UpColor : DownColor;

      // 上影线（矩形，宽度与K线一致）
      if(ObjectFind(0, wick_high_name) < 0)
      {
         ObjectCreate(0, wick_high_name, OBJ_RECTANGLE, 0, block_time, block_high, block_end, MathMax(block_open, block_close));
         ObjectSetInteger(0, wick_high_name, OBJPROP_COLOR, wick_color);
         ObjectSetInteger(0, wick_high_name, OBJPROP_BACK, true);
         ObjectSetInteger(0, wick_high_name, OBJPROP_FILL, true);
         ObjectSetInteger(0, wick_high_name, OBJPROP_SELECTABLE, false);
         ObjectSetInteger(0, wick_high_name, OBJPROP_SELECTED, false);
         ObjectSetInteger(0, wick_high_name, OBJPROP_HIDDEN, true);
      }
      else
      {
         ObjectMove(0, wick_high_name, 0, block_time, block_high);
         ObjectMove(0, wick_high_name, 1, block_end, MathMax(block_open, block_close));
      }

      // 下影线（矩形，宽度与K线一致）
      if(ObjectFind(0, wick_low_name) < 0)
      {
         ObjectCreate(0, wick_low_name, OBJ_RECTANGLE, 0, block_time, MathMin(block_open, block_close), block_end, block_low);
         ObjectSetInteger(0, wick_low_name, OBJPROP_COLOR, wick_color);
         ObjectSetInteger(0, wick_low_name, OBJPROP_BACK, true);
         ObjectSetInteger(0, wick_low_name, OBJPROP_FILL, true);
         ObjectSetInteger(0, wick_low_name, OBJPROP_SELECTABLE, false);
         ObjectSetInteger(0, wick_low_name, OBJPROP_SELECTED, false);
         ObjectSetInteger(0, wick_low_name, OBJPROP_HIDDEN, true);
      }
      else
      {
         ObjectMove(0, wick_low_name, 0, block_time, MathMin(block_open, block_close));
         ObjectMove(0, wick_low_name, 1, block_end, block_low);
      }

      // K线实体（矩形，红/绿）
      if(ObjectFind(0, body_name) < 0)
      {
         ObjectCreate(0, body_name, OBJ_RECTANGLE, 0, block_time, block_open, block_end, block_close);
         ObjectSetInteger(0, body_name, OBJPROP_COLOR, body_color);
         ObjectSetInteger(0, body_name, OBJPROP_BACK, true);
         ObjectSetInteger(0, body_name, OBJPROP_FILL, true);
         ObjectSetInteger(0, body_name, OBJPROP_SELECTABLE, false);
         ObjectSetInteger(0, body_name, OBJPROP_SELECTED, false);
         ObjectSetInteger(0, body_name, OBJPROP_HIDDEN, true);
      }
      else
      {
         ObjectMove(0, body_name, 0, block_time, block_open);
         ObjectMove(0, body_name, 1, block_end, block_close);
      }
   }
}